package com.bcnapps.DemoGame;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.SurfaceHolder.Callback;
import android.view.View.OnTouchListener;

public class GameTwo extends SurfaceView {
	private SurfaceHolder holder;
	private GTwoDrawThread gtdt;
	
	//classes auxiliars
	private Nuvols nuvols;
	//control thread
	private boolean surfacecreated;
	private boolean threadrunning;
	//fons
	private Paint fons;
	
	//joc
	private boolean fingerdown;
	final float SPEED = 10;
	private Plataformes plats;
	private Protagonista prota;
	private float dx;
	
	public GameTwo(Context context) {
		super(context);
		init();
	}

	public GameTwo(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public GameTwo(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	private void init() {
		surfacecreated = false;
		threadrunning = false;
		holder = getHolder();
		holder.addCallback( new Callback() {
			
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				stopthread();
				surfacecreated = false;
			}
			
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				holder.setFormat(PixelFormat.RGBA_8888);
				
				dx = 0;
				fingerdown = false;
					
				fons = new Paint(); 
			    Shader shader = new LinearGradient(0,0,GV.Screen.metrics.widthPixels,GV.Screen.metrics.heightPixels,new int[]{0xFFA4D8FF,0xFF0031D2},null,Shader.TileMode.CLAMP);
			    fons.setShader(shader);  
				
			    prota = new Protagonista(getResources(),SPEED);
			    plats = new Plataformes(getResources());
			    
				nuvols = new Nuvols(getResources());
				surfacecreated = true;
			}
			
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
				startthread();
			}
		});  
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawRect(0,0,getWidth(),getHeight(), fons);
		nuvols.actualitza();
		nuvols.draw(canvas);
		if(fingerdown) prota.salta();
		prota.actualitza(plats);
		prota.draw(canvas);
		plats.draw(canvas, prota.getX()-100, getWidth());
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == event.ACTION_DOWN) {
			prota.salta();
			fingerdown = true;
			return true;
		}
		if(event.getAction() == event.ACTION_UP) {
			prota.salta();
			fingerdown = false;
			return true;
		}
		return super.onTouchEvent(event);
	}
	
	public void startthread() {
		if(!surfacecreated) return;
		if(threadrunning) return;
		gtdt = new GTwoDrawThread(this);
		gtdt.setRunning(true);
		gtdt.start();
		threadrunning = true;
	}
	
	public void stopthread() {
		if(!surfacecreated) return;
		if(!threadrunning) return;
		boolean retry = true;
		while (retry) {
			try {
				gtdt.setRunning(false);
				gtdt.join();
				threadrunning = false;
				retry = false;
				
			} catch (InterruptedException e) {}
		}
	}
}
